// pages/invite/store.js
import { Team } from '../../model/team.js';
var team = new Team();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: false,
    team_id: '',
    name: '',
    phone: '',
    desc: '',
    loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const scene = decodeURIComponent(options.scene)
    if (scene && scene != 'undefined'){
      wx.showLoading({
        title: '加载中',
      });
      this.getTeamUser(scene)
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '二维码信息为空!',
        showCancel: false,
        confirmText: '返回首页',
        confirmColor: '#F3474E',
        success:function(res){
          if (res.confirm){
            wx.switchTab({
              url: '/pages/home/home',
            })
          }
        }
      });
    }
  },
  /**
   * 创建团员
   */
  createTeamUser() {
    if (this.data.loading) {
      return false
    }
    this.setData({
      loading: true
    })
    var data = {
      team_id: this.data.team_id,
      name: this.data.name,
      phone: this.data.phone,
      desc: this.data.desc,
    }
    team.createTeamUser(data,(isVild,result)=>{
      if(isVild) {
        wx.showModal({
          title: '温馨提示',
          content: '绑定成功!',
          showCancel: false,
          confirmText: '返回首页',
          confirmColor: '#F3474E',
          success:function(res){
            if (res.confirm){
              wx.switchTab({
                url: '/pages/home/home',
              })
            }
          }
        });
      }else {
        this.setData({
          loading: false
      })
        for (var tmp in result.msg) {
          wx.showToast({
            title: result.msg[tmp],
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 查看当前用户的店员状态
   */
  getTeamUser(scene){
    team.checkTeamUser({scene: scene},(isVild,result)=>{
      if(isVild) {
        this.setData({
          team_id: result.team_id,
          hidden: true,
        });
        wx.hideLoading();
      } else {
        this.setData({
          loading: false
      })
        for (var tmp in result.msg) {
          wx.showModal({
            title: '温馨提示',
            content: result.msg[tmp],
            showCancel: false,
            confirmText: '返回首页',
            confirmColor: '#F3474E',
            success:function(res){
              if (res.confirm){
                wx.switchTab({
                  url: '/pages/home/home',
                })
              }
            }
          });
          return false
        }
      }
    })
  }
})
